package com.xk.ucs.common;

import cn.hutool.core.util.ArrayUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ErrorEnum
 * @Description 错误码枚举
 * @Author 01387843
 * @Date 2022/6/22
 **/
public enum ErrorEnum implements IError {
    FAIL_500("500","[处理异常]%s"),
    NETWORK_ERROR_500("500","网络异常"),

    NOT_EXIST("10000","%s不存在"),
    PARAM_INVALID("10001","参数不合法"),
    PARAM_CONFIG_INVALID("10001","参数配置错误"),

    USER_NOT_EXIST("10201","用户不存在","用户不存在"),
    USER_OR_PASSWORD_EXIST("10200","账号或密码错误或用户不存在","账号或密码错误"),
    USER_ACCOUNT_REPEAT("10201","用户账号%s重复","用户账号%s已存在"),
    USER_BOX_PERMISSION_DENY("10201","用户没有该盒子的权限","用户没有该盒子的权限"),
    USER_NOT_AUTHED("10002","用户无权限","用户无权限"),
    TOKEN_EMPTY("10003","TOKEN为空","TOKEN不能为空"),
    TOKEN_INVALID("10004","TOKEN不合法","TOKEN不合法"),
    PARAM_ERROR("10005","系统参数错误%s",""),
    PARAM_VALID_ERROR("10006","参数校验失败",""),
    /**
     * 下位机交互异常以2开头
      */
    MACHINE_EXCEPTION("2001","下位机故障"),
    GOODS_OUT_EXCEPTION("2002","取货异常"),
    GOODS_OUT_TIME_OUT("2003","取货超时"),
    SCAN_CHANNEL_TIME_OUT("2004","货道扫描超时"),
    SCAN_CHANNEL_FAILURE("2005","货道扫描失败"),
    SCAN_CHANNEL_EXCEPTION("2006","货道扫描异常"),
    REFUND_TIME_OUT("2007","退货超时"),

    TOKEN_EXPIRED("50001","token已过期，请重新登录"),
    AFTER_SALE_ORDER_NOT_EXIST("3001","未找到%s对应订单哦"),
    AFTER_SALE_ORDER_EXPIRED("3002","订单下单日期为 %s，已过7天退货期限"),
    AFTER_SALE_ALREADY("3003","当前订单%s已经退货"),
    AFTER_SALE_ERROR("3004","退货机械异常"),
    SYS_ERROR("500","系统异常","请联系管理员处理")
    ;
    private String code="500";
    private String message = "系统异常，请联系管理员";
    private String info = "系统错误";

    private static Map<String, String> infoMap = new HashMap<>();
    private static Map<String, String> messageMap = new HashMap<>();

    ErrorEnum() {
        this.code = "500";
        this.message = "系统异常，请联系管理员";
        this.info = "系统错误";
    }

    static {
        for (ErrorEnum v:values()){
            infoMap.put(v.code,v.info);
        }
        for (ErrorEnum v:values()){
            messageMap.put(v.code,v.message);
        }
    }

    public static String getInfo(String code){
        return infoMap.get(code);
    }
    public static String getMessage(String code){
        return messageMap.get(code);
    }
    public ErrorEnum setMessage(String message){
        this.message = message;
        return this;
    }

    ErrorEnum(String code, String message, String info) {
        this.code = code;
        this.message = message;
        this.info = info;
    }
    ErrorEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public ApplicationException exp() {
        return new ApplicationException(this.code, this.message);
    }

    @Override
    public ApplicationException exp(Object... args) {
        return new ApplicationException(this.code, fmtExpMsg(this.getMessage(), args), args);
    }

    @Override
    public ApplicationException exp(Throwable cause, Object... args) {
        return new ApplicationException(this.code, fmtExpMsg(this.getMessage(), args), args, cause);
    }

    @Override
    public ApplicationException expMsg(String message, Object... args) {
        return new ApplicationException(this.code, fmtExpMsg(message, args), args);
    }

    @Override
    public ApplicationException expMsg(String message, Throwable cause, Object... args) {
        return new ApplicationException(this.code, fmtExpMsg(message, args), args, cause);
    }

    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public String getMessage() {
        return this.message;
    }
    @Override
    public String getInfo() {
        return this.info;
    }

    public static String fmtExpMsg(String message, Object... args) {
        String formattedMsg = message;
        if (ArrayUtil.isNotEmpty(args) && message != null) {
            formattedMsg = String.format(message, args);
        }
        return formattedMsg;
    }
}
